package tms.business.jabber;

import tms.business.time.TimeService;
import tms.util.jabber.JabberManager;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.Date;

@Stateless
public class JabberStatusAutostopTrigger {
    @EJB
    TimeService timeService;
    @Inject
    JabberManager jabberManager;


    @Schedule(second = "*/10", minute="*", hour="*")
    public void trigger() {
        for (JabberManager.JabberUser jabberUser : jabberManager.getJabberUsers()) {
            if (jabberUser.getStatus() == jabberUser.getPauseStatus() && !jabberUser.isPaused()) {
                timeService.pauseTime(jabberUser.getUserSysId());
                jabberManager.pauseUser(jabberUser.getJabberId());
            }
            if (jabberUser.getStatus() != jabberUser.getPauseStatus() && jabberUser.isPaused()) {
                timeService.playTime(jabberUser.getUserSysId(), new Date());
                jabberManager.unpauseUser(jabberUser.getJabberId());
            }
        }
    }
}
